package com.crm.system.controller.userForumManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.userForumManager.UserForumReply;
import com.crm.model.vo.userForumManager.UserForumReplyVO;
import com.crm.model.vo.userForumManager.UserForumSonReplyVO;
import com.crm.system.service.userForumManager.UserForumReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/5/24 9:34
 */
@RestController
@RequestMapping("/userForumReply")
@Slf4j
public class UserForumReplyController {

    @Autowired
    private UserForumReplyService userForumReplyService;

    /**
     * 添加评论
     * @param userForumReply
     * @return
     */
    @PostMapping("/addForumReply")
    public DataResponse addForumReply(@RequestBody UserForumReply userForumReply){
        UserForumReplyVO vo = userForumReplyService.addForumReply(userForumReply);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
    }

    /**
     * 删除评论
     * @param userForumReplyId
     * @return
     */
    @PostMapping("/delForumReply")
    public DataResponse delForumReply(@RequestParam String userForumReplyId){
        boolean b = userForumReplyService.delForumReply(userForumReplyId);
        if (!b){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 查看评论
     * @param userForumId
     * @return
     */
    @PostMapping("/selectForumReply")
    public DataResponse selectForumReply(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                         @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                         String userForumId){
        DhPageInfo<UserForumReplyVO> vo = userForumReplyService.selectForumReply(pageIndex,pageSize,sortName,sortOrder,userForumId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
    }

    /**
     * 查看子评论
     * @param userForumReplyId
     * @return
     */
    @PostMapping("/selectForumSonReply")
    public DataResponse selectForumSonReply(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                            @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                            @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                            String userForumReplyId,
                                            String userForumId){
        DhPageInfo<UserForumReplyVO> vo = userForumReplyService.selectForumSonReply(pageIndex,pageSize,sortName,sortOrder,userForumReplyId,userForumId);
        UserForumSonReplyVO resultVo = new UserForumSonReplyVO();
        if (Objects.nonNull(vo)){
            resultVo = userForumReplyService.selectForumReplyById(vo.getList().get(0).getOneReplyId());
            if (Objects.isNull(resultVo)){
                resultVo = new UserForumSonReplyVO();
            }
            resultVo.setSonReplyList(vo);
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),resultVo);
    }
}
